function formatDateTime(date) {
    if(date == null){
        return '';
    }else{
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        return y + '-' + m + '-' + d;
    }
}

function getTimeFormatData(data) {
    if (data >= 0 && data <= 9) {
        data = "0" + data;
    }
    return data;
}

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    var day = getTimeFormatData(date.getDate());
    var hour = getTimeFormatData(date.getHours());
    var minute = getTimeFormatData(date.getMinutes());
    var second = getTimeFormatData(date.getSeconds());
    return date.getFullYear() + seperator1 + month + seperator1 + day
        + " " + hour + seperator2 + minute + seperator2 + second;
}

function getTakingTime(date1, date2){
    var date3=date2.getTime()-date1.getTime();  //时间差的毫秒数
    //计算出相差天数
    var days=Math.floor(date3/(24*3600*1000));
    //计算出小时数
    var leave1=date3%(24*3600*1000);    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000));
    //计算相差分钟数
    var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000));
    return (days > 0 ? days + "天" : "") + (hours > 0 ? hours + "小时" : "") + (minutes > 0 ? minutes + "分钟" : "");
}

function getTakingTimeBySeconds(second){
    //计算出相差天数
    var days=Math.floor(second/(24*3600));
    //计算出小时数
    var leave1=second%(24*3600);    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600));
    //计算相差分钟数
    var leave2=leave1%(3600);        //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60));
    var second = Math.floor(leave2%(60));
    return (days > 0 ? days + "天" : "") + (hours > 0 ? hours + "小时" : "") + (minutes > 0 ? minutes + "分钟" : "")
        + (second > 0 ? second + "秒" : "");
}

function getTakingDays(date1, date2){
    var d1 = date1.getDate();
    var d2 = date2.getDate();
    var result = d2 - d1;
    return result > 5 ? 1 : result;
}

function getTakingTimes(date1, date2){
    var t1 = date1.getTime();
    var t2 = date2.getTime();
    var nTime = t2 - t1;
    //计算出相差天数
    var days=Math.floor(nTime/(24*3600*1000));
    //计算出小时数
    var leave1=nTime%(24*3600*1000);    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000));
    //计算相差分钟数
    var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000));
    return (days > 0 ? days + "天" : "") + (hours > 0 ? hours + "小时" : "") + (minutes > 0 ? minutes + "分" : "");
}

function getTakingHours(date1, date2){
    var t1 = date1.getTime();
    var t2 = date2.getTime();
    var nTime = t2 - t1;
    return nTime / 1000 / 60 / 60;
}

function getTakingMinutes(date1, date2){
    var t1 = date1.getTime();
    var t2 = date2.getTime();
    var nTime = t2 - t1;
    return nTime / 1000 / 60;
}

function bDateBeyondNow(sDate) {
    sDate = sDate.replace(/-/g, "/");//替换字符，变成标准格式
    var now = new Date();
    sDate = new Date(Date.parse(sDate));
    return sDate > now;
}

function getSpecDate(date, num){
    var day = date.getDate();
    date.setDate(day + num);
    return formatDateTime(date);
}

function getWeekDate(date){
    var weekData = ['日', '一', '二', '三', '四', '五', '六'];
    return '周' + weekData[date.getDay()];
}

function getBirthdatByIdNo(iIdNo) {
    var tmpStr = "";
    var strReturn = "";
    if (!iIdNo) {
        return '';
    }
    if ((iIdNo.length != 15) && (iIdNo.length != 18)) {
        strReturn = "输入的身份证号位数错误";
        return strReturn;
    }
    if (iIdNo.length == 15) {
        tmpStr = iIdNo.substring(6, 12);
        tmpStr = "19" + tmpStr;
        tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)

        return tmpStr;
    } else {
        tmpStr = iIdNo.substring(6, 14);
        tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)

        return tmpStr;
    }
}

function getPassengerType(birth) {
    var r = birth.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if (r == null) return false;
    var d = new Date(r[1], r[3] - 1, r[4]);
    if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
        var Y = new Date().getFullYear();
        var age = Y - r[1];
        if( age <= 2 ){
            return 3;
        }else if( age <= 12 ){
            return 2;
        }else{
            return 1;
        }
    }
    return 1;
}

function bCanTakeChild(strBirthday, departDate)
{
    var age = calCulateAge(strBirthday, departDate);
    if (age < 18) {
        return false;
    } else {
        return true;
    }
}

function isErrorBirth(strBirthday, today) {
    var age = calCulateAge(strBirthday, today);
    if (age < 0) {
        return true;
    } else {
        return false;
    }
}

function iGetSexByIdCard(sIdNo)
{
    var sChar = sIdNo.substr(16, 1);
    sChar = parseInt(sChar);
    // 0是保密 1是男 2是女
    if (!isNaN(sChar)) {
        if (sChar % 2 === 1) {
            return 1;
        } else {
            return 2;
        }
    } else {
        return 0;
    }
}

function isErrorValidity(startDate, endDate) {
    startDate = startDate.replace(/-/g, "/");
    endDate = endDate.replace(/-/g, "/");
    var sDate = getTakingTime(new Date(startDate), new Date(endDate));
    var days = sDate.match(/^(\d+)天/);
    if( days ) return days[1] < 183 ? true : false;
    return true;
}

function isChild(strBirthday, departDate) {
    var age = calCulateAge(strBirthday, departDate);
    if(age >= 2 && age < 12){
        return true;
    }else{
        return false;
    }
}

function isBaby(strBirthday, departDate){
    var age = calCulateAge(strBirthday, departDate);
    if(age > -1 && age < 2){
        return true;
    }else{
        return false;
    }
}

function calCulateAge(strBirthday, departDate) {
    var returnAge;
    var strBirthdayArr = strBirthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];
    departDate = departDate.replace(/-/g, "/");
    var d = new Date(departDate);
    var nowYear = d.getFullYear();
    var nowMonth = d.getMonth() + 1;
    var nowDay = d.getDate();
    if (nowYear == birthYear) {
        returnAge = 0;//同年 则为0岁
        if (nowMonth == birthMonth) {
            returnAge = 0;//同年 通月
            if (nowDay == birthDay) {
                returnAge = 0;//同年 同月 同日
            } else if (nowDay < birthDay) {
                returnAge = -1;//输入日期错误
            }
        } else if (nowMonth < birthMonth) {
            returnAge = -1;//输入日期错误
        }
    }
    else {
        var ageDiff = nowYear - birthYear; //年之差
        if (ageDiff > 0) {
            if (nowMonth == birthMonth) {
                var dayDiff = nowDay - birthDay;//日之差
                if (dayDiff < 0) {
                    returnAge = ageDiff - 1;
                }
                else {
                    returnAge = ageDiff;
                }
            }
            else {
                var monthDiff = nowMonth - birthMonth;//月之差
                if (monthDiff < 0) {
                    returnAge = ageDiff - 1;
                }
                else {
                    returnAge = ageDiff;
                }
            }
        }
        else {
            returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
        }
    }
    return returnAge;
}

function IdentityCodeValid(ID) {
    if (typeof ID !== 'string') return false;
    ID = ID.toUpperCase();
    var city = {11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外"};
    var birthday = ID.substr(6, 4) + '/' + Number(ID.substr(10, 2)) + '/' + Number(ID.substr(12, 2));
    var d = new Date(birthday);
    var newBirthday = d.getFullYear() + '/' + Number(d.getMonth() + 1) + '/' + Number(d.getDate());
    var currentTime = new Date().getTime();
    var time = d.getTime();
    var arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    var arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
    var sum = 0, i, residue;

    if (!/^\d{17}(\d|x)$/i.test(ID)) return false;
    if (city[ID.substr(0, 2)] === undefined) return false;
    if (time >= currentTime || birthday !== newBirthday) return false;
    for (i = 0; i < 17; i++) {
        sum += ID.substr(i, 1) * arrInt[i];
    }
    residue = arrCh[sum % 11];
    if (residue !== ID.substr(17, 1)) return false;

    return true;
}

function toHourMinute(minutes){
    return (Math.floor(minutes/60) + "小时" + (minutes%60) + "分" );
}